package com.htudata.grid.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.grid.dao.GridDateDao;
import com.htudata.grid.service.GridDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GridDateServiceImpl implements GridDateService {

  @Autowired
  private GridDateDao gridDateDao;

  @Override
  public JSONArray getAllReach()  throws Exception{
    JSONArray result = this.gridDateDao.getAllReach();
    return result;
  }

  @Override
  public String getAllReachJSON()  throws Exception{
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_006.getCode(), getAllReach());
    } catch (Exception e) {
       throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }

  @Override
  public JSONArray getEachReach() throws Exception {
    return this.gridDateDao.getEachReach();
  }

  @Override
  public String getEachReachJSON() throws Exception{
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_008.getCode(), getEachReach());
    } catch (Exception e) {
      throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }

  @Override
  public JSONArray getEachProportion()throws Exception {
    return this.gridDateDao.getEachProportion();
  }

  @Override
  public String getEachProportionJSON()throws Exception {
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_007.getCode(), getEachProportion());
    } catch (Exception e) {
      throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }

  @Override
  public JSONArray getRainFctImgByFct(String fct)throws Exception {
    return this.gridDateDao.getRainFctImgByImg(fct);
  }

  @Override
  public String getRainFctImgByFctJSON(String fct) throws Exception{
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_022.getCode(), getRainFctImgByFct(fct));
    } catch (Exception e) {
      throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }

  @Override
  public JSONArray getGridDetails(long dataTime)throws Exception {
    return this.gridDateDao.getGridDetails(dataTime);
  }

  @Override
  public String getGridDetailsJSON(long dataTime)throws Exception {
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_023.getCode(), getGridDetails(dataTime));
    } catch (Exception e) {
      throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }

  @Override
  public JSONArray getTimeFileList(String future, String fct, int offset, int rows)throws Exception {
    JSONArray jsonArray = null;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    BigInteger nowDate = null;
    BigInteger endDate = null;
    if ("48h".equalsIgnoreCase(future)) {
      nowDate = BigInteger.valueOf(Long.parseLong(LocalDateTime.now().plusDays(1L).format(formatter)));
      endDate = BigInteger.valueOf(Long.parseLong(LocalDateTime.now().plusDays(2L).format(formatter)));
    } else if ("72h".equalsIgnoreCase(future)) {
      nowDate = BigInteger.valueOf(Long.parseLong(LocalDateTime.now().plusDays(2L).format(formatter)));
      endDate = BigInteger.valueOf(Long.parseLong(LocalDateTime.now().plusDays(3L).format(formatter)));
    } else {
      nowDate = BigInteger.valueOf(Long.parseLong(LocalDateTime.now().format(formatter)));
      endDate = BigInteger.valueOf(Long.parseLong(LocalDateTime.now().plusDays(1L).format(formatter)));
    } 
    jsonArray = this.gridDateDao.getTimeFileList(nowDate, endDate, fct, offset, rows);
    return jsonArray;
  }

  @Override
  public String getTimeFileListJSON(String future, String fct, int offset, int rows)throws Exception {
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_020.getCode(), getTimeFileList(future, fct, offset, rows));
    } catch (Exception e) {
      throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }
}
